package com.mk.admin.service.impl;

import com.mk.admin.mapper.*;
import com.mk.admin.model.*;
import com.mk.admin.model.dto.*;
import com.mk.admin.model.entity.*;
import com.mk.admin.model.query.*;
import com.mk.admin.model.vo.*;
import com.mk.admin.service.*;
import com.mk.admin.util.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;

@Service
public class MKCheckServiceImpl implements MKCheckService {

    @Autowired
    private ChinaDrugKindMapper cnKindMapper;
    @Autowired
    private ChinaDrugPrescAuthMapper cnPrescAuthMapper;
    @Autowired
    private ChinaDrugDupService cnDupService;
    @Autowired
    private ChinaDrugDoseLowService cndDoseLowService;
    @Autowired
    private ChinaDrugDiService cndDiService;

    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public ResultDto select(MKCheckVo vo) {
        List<MKCheckDto> checkList = new ArrayList<>();
        ChinaDrugPrescAuthEntity auth = JSONCopy.copy(vo, ChinaDrugPrescAuthEntity.class);
        ChinaDrugKindEntity kind = JSONCopy.copy(vo, ChinaDrugKindEntity.class);
        ChinaDrugDupEntity dupEntity = JSONCopy.copy(vo, ChinaDrugDupEntity.class);
        DoctorEntity doctorEntity = new DoctorEntity();

        List<ChinaDrugKindDto> Kindlist = cnKindMapper.select(kind);
        List<ChinaDrugPrescAuthDto> authList = cnPrescAuthMapper.select(auth);
        ChinaDrugDupDto dup = null;
        ChinaDrugDiDto di = null;
        ChinaDrugDoseLowDto doseLow = null;

        //管控方式说明
        String[] control = {"", "阻断开单", "填写原因后开单", "确认后开单", "提示"};
        //严重程度说明
        String[] level = {"", "禁用", "忌用", "慎用", "提示"};

        //装配医嘱信息
        List orderList = new ArrayList();
        vo.getOrder().forEach(v -> {
            MKOrderDto order = new MKOrderDto();
            order.setCode(v.getCode());
            order.setName(v.getName());
            order.setId(v.getId());
            order.setUsage(v.getUsage());
            orderList.add(order);
        });

        //非有毒中药材最低剂量
        doseLow = cndDoseLowService.select(vo);
        if (doseLow.getDescribe() != null) {
            MKCheckDto dto = new MKCheckDto();
            dto.setTitle("非有毒中药材最低剂量");
            MKModeDto mode = new MKModeDto();
            mode.setCite(doseLow.getCite());
            mode.setWay(control[Integer.parseInt(doseLow.getWay())]);
            mode.setLevel(level[Integer.parseInt(doseLow.getLevel())]);
            mode.setDesc(doseLow.getDescribe());
            dto.setOrders(orderList);
            dto.setMode(mode);
            checkList.add(dto);
        }


        //中成药与中药材成分重复 by BSC
        if (vo.getOrder().size() >= 2) {
            MKCheckDto dto = new MKCheckDto();
            dup = cnDupService.select(vo);

            if (dup.getDrug() != null) {
                dto.setTitle("中成药与中药材成分重复");
                MKModeDto mode = new MKModeDto();
                mode.setCite(dup.getCite());
                mode.setWay(control[Integer.parseInt(dup.getWay())]);
                mode.setLevel(level[Integer.parseInt(dup.getLevel())]);
                mode.setDesc(dup.getDescribe());
                dto.setOrders(orderList);
                dto.setMode(mode);
                checkList.add(dto);
            }

        }

        //中药（成分）相互作用 by BSC
        if (vo.getOrder().size() >= 2) {
            MKCheckDto dto = new MKCheckDto();
            di = cndDiService.select(vo);
            if (di.getDrugA() != null) {
                dto.setTitle("十八反十九畏");
                MKModeDto mode = new MKModeDto();
                mode.setCite(di.getCite());
                mode.setWay(control[Integer.parseInt(di.getWay())]);
                mode.setLevel(level[Integer.parseInt(di.getLevel())]);
                mode.setDesc(di.getDescribe());
                dto.setOrders(orderList);
                dto.setMode(mode);
                checkList.add(dto);
            }
        }

        //中药处方权
        if (authList.get(0).getEnable() == 1) { //启用规则 进入逻辑
            String doctorCode = vo.getDoctorCode();
            doctorEntity.setCode(doctorCode);
            List<DoctorDto> list = doctorMapper.select(doctorEntity);
            //判断中药房的编码
            if (list.get(0).getCode() != "") {
                MKCheckDto dto = new MKCheckDto();
                dto.setTitle("中药处方权");
                MKModeDto mode = new MKModeDto();
                mode.setCite(authList.get(0).getCite());
                mode.setWay(control[Integer.parseInt(authList.get(0).getWay())]);
                mode.setLevel(level[Integer.parseInt(authList.get(0).getLevel())]);
                mode.setDesc(authList.get(0).getDescribe());
                dto.setOrders(orderList);
                dto.setMode(mode);
                checkList.add(dto);
            }
        }

        //中药超品种管控
        Integer amount = Integer.parseInt(Kindlist.get(0).getAmount());
        if (Kindlist.get(0).getEnable() == 1 && vo.getOrder().size() > amount) { //启用规则 进入逻辑
            MKCheckDto dto = new MKCheckDto();
            List data = new ArrayList();
            dto.setTitle("中药超品种管控");
            MKModeDto mode = new MKModeDto();
            mode.setCite(Kindlist.get(0).getCite());
            mode.setWay(control[Integer.parseInt(Kindlist.get(0).getWay())]);
            mode.setLevel(level[Integer.parseInt(Kindlist.get(0).getLevel())]);
            String str = "中药一张处方仅能开具" + amount + "个品种的药品";
            mode.setDesc(str);
            dto.setOrders(orderList);
            dto.setMode(mode);
            checkList.add(dto);
        }
        return ResultDto.data(checkList);
    }
}
